layui.config({
    base: "/layui/"
}).extend({
    index: 'lib/index'
}).use(['index', 'form', 'jquery', 'table', 'upload'], function () {
    var form = layui.form,
        table = layui.table,
        upload = layui.upload,
        admin = layui.admin;

    /**
     * 防重复设置
     * @type {boolean}
     */
    var edit_flag = true;

    table.render({
        elem: '#device_batch_info_table'
        , url: '/deviceBatch/queryData'
        , method: "post"
        , height: 500
        , limit: 50							//显示数目
        , limits: [50, 100]
        , cols: [[
            {field: 'id', title: 'id', width: 80, sort: true, "align": "center"}
            , {field: 'producer', width: 100, title: '设备厂家', "align": "center"}
            , {field: 'deviceType', width: 100, title: '货物类型', "align": "center", templet: function (d) {
                    if (d.deviceType == "WECHAT") {
                        return "微信";
                    } else if (d.deviceType == "ALIPAY") {
                        return "支付宝";
                    } else{
                        return "";
                    }
                }}
            , {field: 'enterType', width: 200, title: '进货类型', "align": "center", templet: function (d) {
                    if (d.enterType == "unified") {
                        return "统一采购";
                    } else if (d.enterType == "personal") {
                        return "个人采购";
                    } else{
                        return "";
                    }
                }}
            , {field: 'count', width: 220, title: '本批数量', "align": "center"}
            , {field: 'createdTime', width: 180, title: '创建时间', "align": "center"}
            , {field: 'OPT', width: 120, title: '操作', "align": "center", toolbar: "#OptData"}
        ]]
        , request: {
            pageName: 'pageNo' //页码的参数名称，默认：page
            , limitName: 'pageSize' //每页数据量的参数名，默认：limit
        },
        where: {}
        , id: 'device_batch_info_table'
        , page: true
    });

    //设备导入按钮事件
    $(".type_btn").click(function () {
        layer.open({
            type: 1,
            area: ['550px', '390px'],
            title: "选择",
            fixed: false, //不固定
            maxmin: true,
            maxmin: false,
            shadeClose: false,
            content: $('#pot_win')
        });
    });

    //批次查询按钮事件
    $(".search_btn").click(function () {
        table.reload('device_batch_info_table', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                enterType: $(".queryTable select[name='queryEnterType']").val(),
                deviceType: $(".queryTable select[name='queryDeviceType']").val(),
                producer: $(".queryTable select[name='queryProducer']").val()
            }
        });
    });

    /**
     * 导入
     */
    var uploadInst = upload.render({
        elem: '#file' //绑定元素
        , url: '/deviceOrder/importExcel'
        , method: 'post'
        , accept: 'file'
        , exts: 'xls'| 'xlsx' //文件格式 | 分割
        , data: {}
        ,before: function(obj){
            this.data.enterType = $("#enterType").val();
            this.data.deviceType = $("#deviceType").val();
            this.data.producer = $("#producer").val();
            obj.preview(function(index, file, result){
                $('#filename').text(file.name);//显示文件名
            });
        }
        , done: function (res) {
            if (res.code == 1) {
                layer.closeAll();
                layer.msg('上传成功', {
                    icon: 6
                });
                table.reload('device_batch_info_table');
            } else {
                layer.msg(res.message, {
                    icon: 5
                });
            }
        }
        , error: function (res) {
            //请求异常回调
            layer.msg('服务器错误', {
                icon: 5
            });
        }
    });

    /**
     * 批次详情
     */
    table.on('tool(device_batch_info_table)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
        var data = obj.data; //获得当前行数据
        var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
        var tr = obj.tr; //获得当前行 tr 的DOM对象
        if (layEvent == 'data') {
            layer.open({
                type: 1,
                area: ['1600px', '650px'],
                title: "批次详情",
                fixed: false, //不固定
                maxmin: true,
                maxmin: false,
                shadeClose: false,
                content: $('#device_order_win')
            });
            //表单值
            form.val("device_batch_form", {
                'deviceBatchId': data.id
            });
            table.render({
                elem: '#department_result'
                , url: '/deviceBatch/queryDeviceOrderData'
                , method: 'post'
                , title: '批次详情'
                , height: 500
                , limit: 50							//显示数目
                , limits: [50, 100]
                , cols: [[
                    {field: 'id', title: 'id', width: 200, "align": "center", hide: true}
                    , {field: 'userName', title: '拥有者', width: 200, "align": "center"}
                    , {field: 'ownerName', title: '买方名称', width: 200, "align": "center"}
                    , {field: 'cameraSn', width: 220, title: '摄像头sn序列号', "align": "center"}
                    , {field: 'terminalSn', title: '设备sn序列号', width: 220, "align": "center"}
                    , {field: 'orgName', title: '归属公司名称', width: 200, "align": "center"}
                    , {field: 'remarks', title: '备注', width: 140, "align": "center"}
                    , {field: 'createdTime', title: '创建时间', width: 180, "align": "center"}
                ]]
                , request: {
                    pageName: 'pageNo' //页码的参数名称，默认：page
                    , limitName: 'pageSize' //每页数据量的参数名，默认：LIMIT
                },
                where: {
                    'deviceBatchId': data.id
                }
                , id: 'department_result'
                , page: true
            });
        };
    });

    //批次详情查询按钮事件
    $(".order_btn").click(function () {
        table.reload('department_result', {
            page: {
                curr: 1 //重新从第 1 页开始
            }
            , where: {
                deviceBatchId: $("#deviceBatchId").val(),
                terminalSn: $("#terminalSn").val(),
                orgName: $("#orgName").val()
            }
        });
    });


});